package cn.zjh.school.controller;

import cn.zjh.base.utils.AjaxResult;
import cn.zjh.base.utils.PageList;
import cn.zjh.school.domain.Student;
import cn.zjh.school.query.StudentQuery;
import cn.zjh.school.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    public IStudentService studentService;


    /**
     * @Title: loadById
     * @Description: 查询所有
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/24 13:18
     * @Parameters:
     * @Return
     */
    @GetMapping()
    public List<Student> loadAll() {
        return studentService.loadAll();
    }

    /**
     * @Title: loadById
     * @Description: 根据id查询
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/24 13:15
     * @Parameters:
     * @Return
     */
    @GetMapping("/{id}")
    public Student loadById(@PathVariable("id") Long id) {
        return studentService.loadById(id);
    }


    /**
     * @Title: remove
     * @Description: 删除
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/24 13:22
     * @Parameters:
     * @Return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            studentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }


    /**
     * @Title: addOrUpt
     * @Description: 新增/修改
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/24 13:26
     * @Parameters:
     * @Return
     */

    @PutMapping()
    public AjaxResult addOrUpt(@RequestBody Student student){
        try {
            if (student.getId() == null){
                studentService.save(student);
            } else {
                studentService.update(student);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }


    /**
     * @Title: remove
     * @Description: 高级查询+分页查询
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/24 13:28
     * @Parameters:
     * @Return
     */
    @PostMapping()
    public PageList<Student> queryData(@RequestBody StudentQuery studentQuery){
        PageList<Student> data = studentService.queryData(studentQuery);
        return data;
    }


    /**
     * @Title: batchDel
     * @Description: 批量删除
     * @Author: Mr.Zhang
     * @Version: 1.0
     * @Date:  2022/6/25 22:12
     * @Parameters:
     * @Return
     */
    @PatchMapping()
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            studentService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统错误，请重试！！！");
        }
    }


    @PostMapping("/{id}")
    public PageList<Student> data(@PathVariable("id") Long id,@RequestBody StudentQuery studentQuery ){

        PageList<Student> data = studentService.getData(id,studentQuery);

        return data;
    }

}
